<template>
	<view class="main">
		<!-- 转链文本框 -->
		<view class="wrap">
			<u--textarea height="150" maxlength="2000" v-model="textValue" placeholder="请输入内容"></u--textarea>
			<view class="button-group">
				<u-button type="warning" text="重置" @click="handleReset"></u-button>
				<u-button type="primary" text="转链" @click="handleSubmit"></u-button>
			</view>
		</view>

		<u-divider>历史记录</u-divider>

		<!-- 转链后的商品 -->
		<view class="goods-list">

			<view class="goods" v-for="item in goodsList" :key="item.item_id" @click.native="handleClick(item.item_id)">
				<image :src="item.item_pic" mode="aspectFit" class="pic"></image>
				<view class="product">
					<view class="title">
						<text class="double-row">
							{{item.item_title}}
						</text>
					</view>
					<view class="num">
						<view class="price">￥{{item.item_end_price}}</view>
						<view class="old">￥{{item.item_price}}</view>

						<view class="coupon-wrap">
							<view class="coupon" v-if="item.coupon_money != 0">{{item.coupon_money}}元券</view>
						</view>
					</view>

					<view class="info">
						<view class="shop">
							<text class="name">{{item.shop_name}}</text>
						</view>
						<view class="sales">月销 {{item.month_sale}}</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 弹窗 -->
		<u-modal :show="show" :title="title" :content='content' :showCancelButton="true" cancelText="取消"
			@confirm="confirm" @cancel="cancel"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				title: "检测到内容！",
				content: "是否进行转链？",
				textValue: "",
				goodsList: [],
			}
		},
		methods: {
			// 读取粘贴板
			getClipboardData() {
				uni.getClipboardData({
					success: (res) => {
						if (res.data.trim()) {
							this.show = true
							this.textValue = res.data
						} else {

						}
					},
					fail(err) {
						console.log(err);
					}
				})
			},
			// 点击确定
			confirm() {
				this.show = false,
					this.handleSubmit()
			},
			// 点击取消
			cancel() {
				this.show = false
			},
			// 重置
			handleReset() {
				this.textValue = ""
			},
			// 转链
			handleSubmit() {
				uni.showLoading({
					title: "转链中...",
				})
				uni.request({
					url: "http://bbx.com/commonAnalyze",
					method: 'GET',
					data: {
						content: this.textValue
					},
					success: (res) => {
						uni.hideLoading()
						if (res.data.code == 200) {
							uni.showToast({
								title: "转链成功！",
								duration: 2000
							});
							this.goodsList.unshift(res.data.data)
							uni.setStorageSync('goodsList', this.goodsList)
						} else {
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							});
						}

					},
				})
			},
			handleClick(itemId) {
				uni.navigateTo({
					url: `/pages/goods/goods?itemId=${itemId}`
				})
			}
		},
		onLoad() {
			let local = uni.getStorageSync('goodsList')
			if (local) {
				this.goodsList = local
			}
		},
		onShow() {
			this.getClipboardData()
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #F6F7F8;
	}

	.main {
		.wrap {
			width: 700rpx;
			margin: 0 auto;
			margin-top: 50rpx;
			margin-bottom: 50rpx;

			.button-group {
				margin-top: 30rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
		}
	}

	.goods {
		width: 686rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		border-radius: 16rpx;
		margin: 0 auto;
		margin-bottom: 20rpx;
		padding: 12rpx;

		.pic {
			width: 220rpx;
			height: 220rpx;
			display: inline-block;
			margin-right: 16rpx;
			border-radius: 8rpx;
		}

		.product {
			.title {
				position: relative;
				width: 426rpx;
				color: #303133;
				font-size: 28rpx;
				font-weight: 600;
				margin-bottom: 18rpx;
			}


			.num {
				display: flex;
				align-items: center;
				margin-bottom: 8rpx;
				position: relative;

				.price {
					color: #FE395E;
					font-size: 32rpx;
					font-weight: bold;
					margin-right: 8rpx;
				}

				.old {
					text-decoration: line-through;
					color: #909399;
					font-size: 24rpx;
					margin-right: 166rpx;
				}

				.sales {
					color: #909399;
					font-size: 22rpx;
				}

				.coupon-wrap {
					position: absolute;
					top: 0;
					right: 12rpx;

					.coupon {
						font-size: 20rpx;
						background: #FE395E;
						padding: 2rpx 12rpx;
						position: relative;
						color: #FFFFFF;
						border-radius: 4rpx;

						&::before {
							position: absolute;
							content: "";
							width: 10rpx;
							height: 10rpx;
							background: #FFFFFF;
							border-radius: 50%;
							left: -6rpx;
							top: 14rpx;
						}

						&::after {
							position: absolute;
							content: "";
							width: 10rpx;
							height: 10rpx;
							background: #FFFFFF;
							border-radius: 50%;
							right: -6rpx;
							top: 14rpx;
						}
					}
				}
			}

			.revert {
				display: flex;
				font-size: 20rpx;
				margin-bottom: 14rpx;

				.commission {
					background: linear-gradient(225deg, #FDAF1C 0%, #FE395E 100%);
					border-radius: 4rpx;
					padding: 2rpx 8rpx;
					color: #fff;
					margin-right: 8rpx;
				}

				.point {
					background: #ffecef;
					border-radius: 4rpx;
					padding: 2rpx 8rpx;
					color: #303133;

					.text {
						margin-left: 8rpx;
						color: #fe395e;
					}
				}
			}

			.info {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.shop {
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.shop-mini {
						width: 20rpx;
						height: 20rpx;
					}

					.name {
						color: #909399;
						font-size: 22rpx;
						font-weight: 400;
					}
				}

				.sales {
					font-size: 22rpx;
					font-weight: 400;
					color: #909399;
				}
			}
		}
	}
</style>
